package com.lwy.qiuchao.vending.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lwy.qiuchao.vending.feign.FieldFeignService;
import com.lwy.qiuchao.vending.vo.FieldVo;
import com.lwy.qiuchao.vending.vo.VendingDetailVo;
import com.lwy.qiuchao.vending.vo.VendingNumVo;
import com.lwy.qiuchao.vending.vo.VendingVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.vending.dao.VendingMachineDao;
import com.lwy.qiuchao.vending.entity.VendingMachineEntity;
import com.lwy.qiuchao.vending.service.VendingMachineService;


@Service("vendingMachineService")
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineDao, VendingMachineEntity> implements VendingMachineService {

    @Autowired
    FieldFeignService feignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VendingMachineEntity> page = this.page(
                new Query<VendingMachineEntity>().getPage(params),
                new QueryWrapper<VendingMachineEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<VendingNumVo> listFieldandvnum(Map<String, Object> params) {
        //TODO fieldids
        List<Long> fieldids = feignService.getFieldids();
        List<VendingNumVo> collect = new ArrayList<>();
        if(params.get("key")!=""){
            String s = (String) params.get("key");
            Long key = new Long(s);
            if(key!=null&&fieldids.contains(key)){
                VendingNumVo vendingNumVo = getVendingNumVo(key);
                collect.add(vendingNumVo);
            }else {
                collect = fieldids.stream().map(fieldid -> {
                    VendingNumVo vendingNumVo = getVendingNumVo(fieldid);
                    return vendingNumVo;
                }).collect(Collectors.toList());
            }
        }else {
            collect = fieldids.stream().map(fieldid -> {
                VendingNumVo vendingNumVo = getVendingNumVo(fieldid);
                return vendingNumVo;
            }).collect(Collectors.toList());
        }


        return collect;
    }

    /**
     * 通过球场id查看贩卖机数目
     * */
    private VendingNumVo getVendingNumVo(Long fieldid) {
        VendingNumVo vendingNumVo = new VendingNumVo();
        Integer num = baseMapper.VendingNumbyFieldid(fieldid);
        vendingNumVo.setFieldId(fieldid);
        vendingNumVo.setVendingNum(num);
        return vendingNumVo;
    }

    @Override
    public List<VendingDetailVo> vendingDetailbyFieldid(Long field) {
        List<VendingMachineEntity> entities = baseMapper.selectList(new QueryWrapper<VendingMachineEntity>().eq("field_id", field));
        List<VendingDetailVo> collect = entities.stream().map(entity -> {
            VendingDetailVo vendingDetailVo = new VendingDetailVo();
            BeanUtils.copyProperties(entity, vendingDetailVo);
            return vendingDetailVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateStatus(VendingMachineEntity vendingMachine) {
        Long vendId = vendingMachine.getVendId();
        Long fieldId = vendingMachine.getFieldId();
        update(vendingMachine,new UpdateWrapper<VendingMachineEntity>().eq("vend_id",vendId).and(item->{
            item.eq("field_id",fieldId);
        }));
    }

    @Override
    public void removeOne(VendingMachineEntity vendingMachine) {
        Long vendId = vendingMachine.getVendId();
        Long fieldId = vendingMachine.getFieldId();
        remove(new QueryWrapper<VendingMachineEntity>().eq("vend_id",vendId).and(item-> {
            item.eq("field_id", fieldId);
        }));
    }

    @Override
    public List<FieldVo> selectFieldandVend() {
        //TODO fieldids
//        List<Long> fieldids = feignService.getFieldids();
        List<Long> fieldids = Arrays.asList(1l,2l,3l);
        List<FieldVo> fieldVos = fieldids.stream().map(fieldid -> {
            FieldVo fieldVo = new FieldVo();
            fieldVo.setId(fieldid);
            fieldVo.setStr(fieldid + "号球场");
            //通过球场id查到所有贩卖机信息 并封装
            List<VendingMachineEntity> entities = baseMapper.selectList(new QueryWrapper<VendingMachineEntity>().eq("field_id", fieldid));
            List<VendingVo> vendingVos = entities.stream().map(item -> {
                VendingVo vendingVo = new VendingVo();
                vendingVo.setId(item.getVendId());
                vendingVo.setStr("第" + item.getVendId() + "台贩卖机");
                return vendingVo;
            }).collect(Collectors.toList());
            fieldVo.setVends(vendingVos);
            return fieldVo;
        }).collect(Collectors.toList());
        return fieldVos;
    }


}